﻿using System;
using System.Data;
using UniMana.Data;
using UniMana.Utils.Data;
using System.Linq;
using UniMana.Utils.Controls;
using DevExpress.XtraGrid;
using UniMana.Utils.Configs;
using UniMana.Utils.Helper;

namespace UniMana.Win.Personnel
{
    public partial class FrmAttendanceUserPermitsion : DevExpress.XtraEditors.XtraForm
    {
        DBMain m_data = new DBMain();

        DataTable tblLeft = new DataTable();
        DataTable tblRight = new DataTable();
        private string m_Rule = UniMana.Utils.Configs.RuleConfig.ATT_DEVISION;

        public FrmAttendanceUserPermitsion()
        {

            if (!SysUtils.CheckAccessByRule(m_Rule))
            {

                return;
            }
            InitializeComponent();
            ////SysUtils.SetRuleToXucBar(xucBar1, this.m_Rule);
            this.LoadData();
        }


        #region [Utils]
        public void LoadData()
        {
            lueCatalogue_Department.GetData(m_data.m_DCReadOnly.Catalogue_Departments);
            rluCatalogue_Department.GetData(m_data.m_DCReadOnly.Catalogue_Departments);
        }
        #endregion

        private void lueCatalogue_Department_EditValueChanged(object sender, EventArgs e)
        {
            tblRight = new DataTable();

            tblLeft = (from p in
                      (
                         from pr in m_data.m_DCReadOnly.Personnel_Personnels
                         where pr.Personnel_PersonnelTypeKey !=2
                         && pr.Catalogue_PersonnelStatusKey != 2
                         select new{
                            pr.Personnel_PersonnelKey,
                            pr.ID,
                            pr.Name,
                            pr.LastName,
                            pr.Catalogue_DepartmentKey
                         })
                      where !(from prds in m_data.m_DCReadOnly.Personnel_AttendanceDevisions
                              where prds.Catalogue_DepartmentKey == lueCatalogue_Department.GetIntValue()
                              select prds.Personnel_PersonnelKey).Contains(p.Personnel_PersonnelKey)
                      select p).ToDataTable();

            gctLeft.DataSource = tblLeft;
            var right = from prd in m_data.m_DCReadOnly.Personnel_AttendanceDevisions
                        join prs in m_data.m_DCReadOnly.Personnel_Personnels
                        on prd.Personnel_PersonnelKey equals prs.Personnel_PersonnelKey
                        where prd.Catalogue_DepartmentKey == lueCatalogue_Department.GetIntValue()
                        where prs.Personnel_PersonnelTypeKey != 2
                        select new
                        {
                            prd.Personnel_PersonnelKey,
                            prs.ID,
                            prs.Name,
                            prs.LastName
                        };

            if (right.Count() == 0)
            {
                tblRight.Columns.Clear();
                DataColumn clr1 = new DataColumn("Personnel_PersonnelKey");
                DataColumn clr2 = new DataColumn("ID");
                DataColumn clr3 = new DataColumn("Name");
                DataColumn clr4 = new DataColumn("LastName");
                tblRight.Columns.AddRange(new DataColumn[] { clr1, clr2, clr3, clr4});
                gctRight.DataSource = tblRight;
                return;
            }

            tblRight = right.ToDataTable(); gctRight.DataSource = tblRight;
        }

        public void MoveItems(GridControl _grcTarget, DevExpress.XtraGrid.Views.Grid.GridView _grvSource)
        {
            if (lueCatalogue_Department.GetIntValue()== -1)
            {
                MsgConfig.ShowErrorDialog("Vui lòng chọn phòng/ban cần phân công chấm công");
            }
            DataTable tbl = _grcTarget.DataSource as DataTable;
            foreach (int i in _grvSource.GetSelectedRows())
            {
                try
                {
                    DataRow newRow = tbl.NewRow();
                    newRow["Personnel_PersonnelKey"] = _grvSource.GetRowCellValue(i, "Personnel_PersonnelKey");
                    newRow["ID"] = _grvSource.GetRowCellValue(i, "ID");
                    newRow["Name"] = _grvSource.GetRowCellValue(i, "Name");
                    newRow["LastName"] = _grvSource.GetRowCellValue(i, "LastName");
                    tbl.Rows.Add(newRow);
                }
                catch{}
            }
            _grvSource.DeleteSelectedRows();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            MoveItems(gctRight, grvLeft);
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            MoveItems(gctLeft, grvRight);
        }

        private void xucBar1_btnSave_Click(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ////if (!UniMana.Lib.Sys.Utils.CheckSaveByRule(this.m_Rule))
            ////{
            ////    xucBar1.dlg.Close();
            ////    return;
            ////}

            Personnel_AttendanceDevisionInfo infoDel = new Personnel_AttendanceDevisionInfo(0,DefaultConfig.EMPTY_GUID);
            infoDel.DeleteByDepartment(lueCatalogue_Department.GetIntValue());

            int deptKey = lueCatalogue_Department.GetIntValue();
            foreach (DataRow r in tblRight.Rows)
            {
                string userKey = r["Personnel_PersonnelKey"].ToString();
                Personnel_AttendanceDevisionInfo infoSave = new Personnel_AttendanceDevisionInfo(deptKey, userKey);
                infoSave.Save();
            }
        }

    }
}